package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.unionwar.UnionQunyingTimeInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.server.logic.unionwar.UnionWarActivityData;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class UnionWarActivityPb {

    public static ActivityProto.ActivityConditionTempMsg.Builder parseActivityConditionTempMsg(ActivityConditionInfo activityConditionInfo, String language){
        if(null == activityConditionInfo){
            return null;
        }
        ActivityProto.ActivityConditionTempMsg.Builder msg = ActivityProto.ActivityConditionTempMsg.newBuilder();
        msg.setActivityId(activityConditionInfo.getActivityId());
        msg.setConditionId(activityConditionInfo.getConditionId());
        msg.setType(activityConditionInfo.getType());
        msg.setReward(activityConditionInfo.getRewardStr());
        if(!StringUtils.isNullOrEmpty(activityConditionInfo.getMasterRewardStr())) {
            msg.setMasterReward(activityConditionInfo.getMasterRewardStr());
        }
        msg.setDesc(ServerLanguageMgr.getContent(activityConditionInfo.getDesc(), language)); // 需要切语言
        msg.setTitle(ServerLanguageMgr.getContent(activityConditionInfo.getTitle(), language));// 需要切语言
        for(BigInteger param : activityConditionInfo.getParamList()){
            msg.addParamList(param.toString());
        }
        msg.setExtendParam(activityConditionInfo.getExtendParam());
        msg.setSubText(ServerLanguageMgr.getContent(activityConditionInfo.getSubText(), language)); // 需要切语言
        msg.setIsOneBuy(ServerLanguageMgr.getContent(activityConditionInfo.getIsOneBuy(), language)); // 需要切语言
        return msg;
    }

    public static ActivityProto.ActivityDataListMsg.Builder parseActivityDataListMsg(List<UnionWarActivityData> baseActivityDataList,
                                                                                     List<ActivityInfo> activityInfoList, Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap,
                                                                                     Map<Integer, Map<Integer, MallInfo>> activityMallInfoMap, Map<Integer, Map<String, ActivityConfig>> activityConfigMap, String language) {
        ActivityProto.ActivityDataListMsg.Builder listMsg = ActivityProto.ActivityDataListMsg.newBuilder();
        for (UnionWarActivityData baseActivityData : baseActivityDataList) {
            if (baseActivityData.getUserActivityConditionDataList().size() > 0) {
                ActivityProto.ActivityDataMsg.Builder dataMsg = parseActivityDataMsg(baseActivityData);
                listMsg.addDataList(dataMsg);
            }
        }

        if (null != activityInfoList) {
            for (ActivityInfo activityInfo : activityInfoList) {
                ActivityProto.ActivityTempMsg.Builder activityMsg = parseActivityTempMsg(activityInfo, language);
                if(null != activityMsg) {
                    listMsg.addActivityList(activityMsg);
                }

                if(null != activityConditionInfoMap){
                    List<ActivityConditionInfo> tempConditionList = activityConditionInfoMap.get(activityInfo.getActivityId());
                    if(null != tempConditionList) {
                        for (ActivityConditionInfo activityConditionInfo : tempConditionList) {
                            ActivityProto.ActivityConditionTempMsg.Builder conditionMsg = parseActivityConditionTempMsg(activityConditionInfo, language);
                            if (null != conditionMsg) {
                                listMsg.addConditionList(conditionMsg);
                            }
                        }
                    }
                }

                if(null != activityMallInfoMap){
                    Map<Integer, MallInfo> tempMallMap = activityMallInfoMap.get(activityInfo.getActivityId());
                    if(null != tempMallMap){
                        for(MallInfo mallInfo : tempMallMap.values()){
                            CommonProto.MallTempMsg.Builder mallMsg = CommonPb.parseMallTempMsg(mallInfo, language);
                            if (null != mallMsg) {
                                listMsg.addActivityMallInfoList(mallMsg);
                            }
                        }
                    }
                }
                if (null != activityConfigMap) {
                    Map<String, ActivityConfig> tempActivityConfigMap = activityConfigMap.get(activityInfo.getActivityId());
                    if (null != tempActivityConfigMap) {
                        for (ActivityConfig activityConfig : tempActivityConfigMap.values()) {
                            ActivityProto.ActivityConfigTempMsg.Builder builder = ActivityPb.parseActivityConfigTempMsg(activityConfig, false ,language);
                            if (null != builder) {
                                listMsg.addConfigList(builder);
                            }
                        }
                    }
                }
            }
        }
        return listMsg;
    }


    public static ActivityProto.ActivityTempMsg.Builder parseActivityTempMsg(ActivityInfo activityInfo, String language) {
        if(null == activityInfo){
            return null;
        }
        ActivityProto.ActivityTempMsg.Builder msg = ActivityProto.ActivityTempMsg.newBuilder();
        msg.setActivityId(activityInfo.getActivityId());
        msg.setResetType(Integer.parseInt(activityInfo.getParam2()));
        msg.setActivityType(activityInfo.getType());
        msg.setChildType(activityInfo.getChildType());
        msg.setBeginShowTime(activityInfo.getBeginShowTime());
        msg.setEndShowTime(activityInfo.getEndShowTime());
        msg.setBeginTime(activityInfo.getBeginTime());
        msg.setEndTime(activityInfo.getEndTime());
        msg.setActivityName(ServerLanguageMgr.getContent(activityInfo.getActivityName(), language)); //需要切语言
        msg.setActivityDesc(ServerLanguageMgr.getContent(activityInfo.getActivityDesc(), language)); //需要切语言
        msg.setConditionText(ServerLanguageMgr.getContent(activityInfo.getParam3(), language)); //需要切语言
        msg.setUnit(ServerLanguageMgr.getContent(activityInfo.getParam4(), language)); //需要切语言
        msg.setIcon(activityInfo.getIcon());
        msg.setPic(activityInfo.getParam1());
        msg.setGoToScene(ServerLanguageMgr.getContent(activityInfo.getParam5(), language));
        msg.setOrder(activityInfo.getOrder());
        msg.setExpandParam(ServerLanguageMgr.getContent(activityInfo.getParam6(), language));
        msg.addAllServerList(activityInfo.getServerIdList());
        msg.setUnionGroup(activityInfo.getUnionGroup());
        return msg;
    }

    public static ActivityProto.ActivityConditionDataMsg.Builder parseActivityConditionDataMsg(UserActivityConditionData userActivityConditionData) {
        if(null == userActivityConditionData){
            return null;
        }
        ActivityProto.ActivityConditionDataMsg.Builder msg = ActivityProto.ActivityConditionDataMsg.newBuilder();
        msg.setConditionId(userActivityConditionData.getConditionId());
        msg.setIsGetReward(userActivityConditionData.isGetReward());
        msg.setValue(userActivityConditionData.getValue().toString());
        msg.setModifyTime(userActivityConditionData.getModifyTime());
        msg.setParam(userActivityConditionData.getParam());
        return msg;
    }

    public static ActivityProto.ActivityDataMsg.Builder parseActivityDataMsg(UnionWarActivityData baseActivityData) {
        if(null == baseActivityData){
            return null;
        }
        ActivityProto.ActivityDataMsg.Builder msg = ActivityProto.ActivityDataMsg.newBuilder();
        msg.setActivityId(baseActivityData.getActivityId());
        for (UserActivityConditionData userActivityConditionData : baseActivityData.getUserActivityConditionDataList()) {
            ActivityProto.ActivityConditionDataMsg.Builder dataMsg = parseActivityConditionDataMsg(userActivityConditionData);
            if(null != dataMsg) {
                msg.addDataList(dataMsg);
            }
        }

        return msg;
    }

    public static UnionWarActivityServerProto.UnionWarTimeInfoSyncMsg.Builder parseActivityConfigData(ActivityInfo activityInfo, List<UnionQunyingTimeInfo> timeInfoList) {
        UnionWarActivityServerProto.UnionWarTimeInfoSyncMsg.Builder synMsg = UnionWarActivityServerProto.UnionWarTimeInfoSyncMsg.newBuilder();
        synMsg.setActivityId(activityInfo.getActivityId());
        for(UnionQunyingTimeInfo timeInfo : timeInfoList){
            UnionWarActivityServerProto.UnionWarTimeInfoTemp.Builder builder = UnionWarActivityServerProto.UnionWarTimeInfoTemp.newBuilder();
            builder.setType(timeInfo.getType());
            builder.setTimeId(timeInfo.getTimeId());
            builder.setSession(timeInfo.getSession());
            builder.setScore(timeInfo.getScore());
            builder.setBeginTime(timeInfo.getBeginTime());
            builder.setDurTime(timeInfo.getDurTime());
            builder.setMatchVideo(timeInfo.getMatchVideo());
            builder.setParam(timeInfo.getParam());
            builder.setRewardsPresiden(timeInfo.getRewardsPresident());
            builder.setRewardsMember(timeInfo.getRewardsMember());
            synMsg.addTimeInfo(builder);
        }
        return synMsg;
    }
}
